using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using TanashCMS.Business;

public partial class preview : CMSPageBase
{
    private WebPage mWebPage = null;

    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
        if (Request.QueryString["template"] != null)
        {
            string masterFile = Request.QueryString["template"] + ".master";
            if (File.Exists(Server.MapPath(Request.ApplicationPath.TrimEnd('/') + "/MasterPages/" + mWebsite.SubFolderName + "/" + masterFile)))
                Page.MasterPageFile = Request.ApplicationPath.TrimEnd('/') + "/MasterPages/" + mWebsite.SubFolderName + "/" + masterFile;
        }

        string id = Request.QueryString["guid"];
        string type = id.Substring(0, 2);
        string guid = id.Substring(2, id.Length - 2);

        switch (int.Parse(type))
        {
            case (int)CMSObjectType.WebPage:
                mWebPage = CMSDataObject.getByGUID<WebPage>(guid);
                break;
            case (int)CMSObjectType.Category:
                break;
            case (int)CMSObjectType.Product:
                break;
        }
    }

    protected override void OnInit(EventArgs e)
    {
        if (mWebPage != null)
        {
            //litHeader.Text = "<h2>" + mWebPage.Title + "</h2>";
            litContent.Text = mWebPage.HTMLContent;
        }

        base.OnInit(e);
    }


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected override void Render(HtmlTextWriter writer)
    {
        // place a preview disclaimer at the top of the page
        HtmlGenericControl preview = new HtmlGenericControl("div");
        preview.Style.Add("width", "100%");
        preview.Style.Add("font-family", "Times New Roman");
        preview.Style.Add("font-size", "10pt");
        preview.Style.Add("font-weight", "bold");
        preview.Style.Add("line-height", "1.5em");
        preview.Style.Add("text-align", "center");
        preview.Style.Add("position", "absolute");
        preview.Style.Add("left", "0");
        preview.Style.Add("top", "0");
        preview.Style.Add("background-color", "#ddd");
        preview.Style.Add("border-bottom", "1px solid #aaa");
        preview.InnerHtml = "Note that this is a preview of your page only.<br/>You must publish your website before your latest changes are visible on your website.<br/>Close this window to return to the site manager";
        Page.Form.Controls.AddAt(0, preview);

        base.Render(writer);
    }
}
